home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15752 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strncpy() ?
  5. Date: 21 Apr 1996 16:44:09 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ldokp$cgl@sparcserver.lrz-muenchen.de>
  9. References: <4ldkvd$553@venus.compulink.gr>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. lonewolf@athena.compulink.gr (Costas Vlassis) writes:
  13.  
  14.  
  15. >    Well I have this question, let's say we have the following :
  16.  
  17. >char alfa[40] = "1123";
  18. >char beta[20];
  19. >char gama[20];
  20.  
  21. >With strncpy(beta,alfa,2) beta = "11" that's fine but HOW can I make 
  22. >gama = "23" that is the last 2 characters of alfa or even "3" the last 
  23. >character... Is there a function that takes start and end as strings ?
  24.  
  25. >what I want is something like :
  26.  
  27. >strncpy2 (beta,alfa,2,4);
  28.  
  29. >    is this possible ?
  30.  
  31. Yes, but not under that name for name conflict reasons (the name is not 
  32. free for your use).
  33.  
  34.    #include <string.h>
  35.  
  36.    char *
  37.    str_ncpy2(char *dest, const char *src, size_t from, size_t to)
  38.    {
  39.       return strncpy(dest, src + from, to - from);
  40.    }
  41.    
  42.   You might want to add error checking to prevent strange results
  43. when "to" is less than "from", or if "src" is not at least "from"
  44. characters long.  
  45.  
  46. Kurt
  47. --
  48. | Kurt Watzka                             Phone : +49-89-2180-6254
  49. | watzka@stat.uni-muenchen.de
  50.  
  51.